package cn.iocoder.yudao.module.forward.controller.admin.firm;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.forward.controller.admin.firm.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.firm.FirmDO;
import cn.iocoder.yudao.module.forward.service.firm.FirmService;

@Tag(name = "管理后台 - 厂商")
@RestController
@RequestMapping("/forward/firm")
@Validated
public class FirmController {

    @Resource
    private FirmService firmService;

    @PostMapping("/create")
    @Operation(summary = "创建厂商")
    @PreAuthorize("@ss.hasPermission('forward:firm:create')")
    public CommonResult<Integer> createFirm(@Valid @RequestBody FirmSaveReqVO createReqVO) {
        return success(firmService.createFirm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新厂商")
    @PreAuthorize("@ss.hasPermission('forward:firm:update')")
    public CommonResult<Boolean> updateFirm(@Valid @RequestBody FirmSaveReqVO updateReqVO) {
        firmService.updateFirm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除厂商")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('forward:firm:delete')")
    public CommonResult<Boolean> deleteFirm(@RequestParam("id") Integer id) {
        firmService.deleteFirm(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得厂商")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('forward:firm:query')")
    public CommonResult<FirmRespVO> getFirm(@RequestParam("id") Integer id) {
        FirmDO firm = firmService.getFirm(id);
        return success(BeanUtils.toBean(firm, FirmRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得厂商分页")
    @PreAuthorize("@ss.hasPermission('forward:firm:query')")
    public CommonResult<PageResult<FirmRespVO>> getFirmPage(@Valid FirmPageReqVO pageReqVO) {
        PageResult<FirmDO> pageResult = firmService.getFirmPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FirmRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出厂商 Excel")
    @PreAuthorize("@ss.hasPermission('forward:firm:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportFirmExcel(@Valid FirmPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FirmDO> list = firmService.getFirmPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "厂商.xls", "数据", FirmRespVO.class,
                        BeanUtils.toBean(list, FirmRespVO.class));
    }

    @GetMapping("/getAllFirm")
    @Operation(summary = "获得所有厂商")
    @PreAuthorize("@ss.hasPermission('forward:firm:query')")
    public CommonResult<List<FirmRespVO>> getAllFirm() {
        List<FirmDO> list = firmService.getAllFirm();
        return success(BeanUtils.toBean(list, FirmRespVO.class));
    }
}